<?php
if (!defined('SMF'))
	die('Hacking attempt...');

global $smcFunc, $txt, $scripturl, $settings, $user_info;
global $dbtables, $sector_max, $bounty_maxvalue;
global $start_energy, $upgrade_factor, $ewd_maxhullsize;
global $bounty_ratio, $bounty_minturns, $level_factor;
global $torp_dmg_rate, $rating_combat_factor, $upgrade_cost;

// Verify user login, if CheckLogin() returns false return to the calling script/function
if (CheckLogin() == false)
	return;

BigTitle($txt['bnt_att_title']);

//-------------------------------------------------------------------------------------------------

/*
$lock = $smcFunc['db_query']('', '
	LOCK TABLES
	{raw:db_ships} WRITE,
	{raw:db_universe} WRITE,
	{raw:db_bounty} WRITE,
	{raw:db_zones} WRITE,
	{raw:db_planets} WRITE,
	{raw:db_news} WRITE,
	{raw:db_logs} WRITE,
	{raw:db_xenobes} WRITE,
	{raw:db_sector_defence} WRITE,
	{raw:db_movement_log} WRITE,
	{raw:db_ibank_accounts} WRITE,
	{raw:db_teams} WRITE',
	array(
		'db_ships' => $dbtables['ships'],
		'db_universe' => $dbtables['universe'],
		'db_bounty' => $dbtables['bounty'],
		'db_zones' => $dbtables['zones'],
		'db_planets' => $dbtables['planets'],
		'db_news' => $dbtables['news'],
		'db_logs' => $dbtables['logs'],
		'db_xenobes' => $dbtables['xenobes'],
		'db_sector_defence' => $dbtables['sector_defence'],
		'db_movement_log' => $dbtables['movement_log'],
		'db_ibank_accounts' => $dbtables['ibank_accounts'],
		'db_teams' => $dbtables['teams']
	)
);
*/

// Retrieve the user and ship information
$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_ships}
	WHERE user_id = {int:user_id}',
	array(
		'db_ships' => $dbtables['ships'],
		'user_id' => $user_info['id'],
	)
);
$playerinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

// Get target information
if (isset($_GET['ship_id']))
	$ship_id = stripnum($_GET['ship_id']);
else
	return;

$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_ships}
	WHERE ship_id = {int:ship_id}',
	array(
		'db_ships' => $dbtables['ships'],
		'ship_id' => $ship_id
	)
);
$targetinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

srand((double)microtime() * 1000000);

$playerscore = gen_score($playerinfo['ship_id']);
$targetscore = gen_score($targetinfo['ship_id']);
$playerscore = $playerscore * $playerscore;
$targetscore = $targetscore * $targetscore;

// Check to ensure target is in the same sector as player
if ($targetinfo['sector'] != $playerinfo['sector'] || $targetinfo['on_planet'] == "Y")
	echo $txt['bnt_att_notarg'], '<br><br>';
elseif($playerinfo['turns'] < 1)
	echo $txt['bnt_att_noturn'], '<br><br>';
else
{
	// Determine percent chance of success in detecting target ship - based on player's sensors and opponent's cloak.
	$success = (10 - $targetinfo['cloak'] + $playerinfo['sensors']) * 5;
	if ($success < 5)
		$success = 5;
	if ($success > 95)
		$success = 95;
		
	$flee = (10 - $targetinfo['engines'] + $playerinfo['engines']) * 5;
	$roll = rand(1, 100);
	$roll2 = rand(1, 100);

	$result = $smcFunc['db_query']('', '
		SELECT allow_attack, {raw:db_universe}.zone_id
		FROM {raw:db_zones}, {raw:db_universe}
		WHERE sector_id = {int:sector_id}
		AND {raw:db_zones}.zone_id = {raw:db_universe}.zone_id',
		array(
			'db_universe' => $dbtables['universe'],
			'db_zones' => $dbtables['zones'],
			'sector_id' => $targetinfo['sector']
		)
	);
	$zoneinfo = $smcFunc['db_fetch_assoc']($result);
	$smcFunc['db_free_result']($result);
	
	if ($zoneinfo['allow_attack'] == 'N')
		echo $txt['bnt_att_noatt'], '<br><br>';
	elseif ($flee < $roll2)
	{
		echo $txt['bnt_att_flee'], '<br><br>';
		$update = $smcFunc['db_query']('', '
			UPDATE {raw:db_ships}
			SET turns = turns - 1,
				turns_used = turns_used + 1
			WHERE ship_id = {int:ship_id}',
			array(
				'db_ships' => $dbtables['ships'],
				'ship_id' => $playerinfo['ship_id']
			)
		);
		playerlog($targetinfo['ship_id'], LOG_ATTACK_OUTMAN, "$playerinfo[character_name]");
	}
	// If scan fails - inform both player and target.
	elseif ($roll > $success)
	{
		echo $txt['bnt_planet_noscan'], '<br><br>';
		$update = $smcFunc['db_query']('', '
			UPDATE {raw:db_ships}
			SET turns = turns - 1,
				turns_used = turns_used + 1
			WHERE ship_id = {int:ship_id}',
			array(
				'db_ships' => $dbtables['ships'],
				'ship_id' => $playerinfo['ship_id']
			)
		);
		playerlog($targetinfo['ship_id'], LOG_ATTACK_OUTSCAN, "$playerinfo[character_name]");
	}
    // If scan succeeds, show results and inform target.
	else
	{
		$shipavg = $targetinfo['hull'] + $targetinfo['engines'] + $targetinfo['power'] + $targetinfo['computer'] + $targetinfo['sensors'] + $targetinfo['armor'] + $targetinfo['shields'] + $targetinfo['beams'] + $targetinfo['torp_launchers'] + $targetinfo['cloak'];
		$shipavg /= 10;
		
		if ($shipavg > $ewd_maxhullsize)
			$chance = ($shipavg - $ewd_maxhullsize) * 10;
		else
			$chance = 0;
		
		$random_value = rand(1,100);
		if ($targetinfo['dev_emerwarp'] > 0 && $random_value > $chance)
		{
			// Need to change warp destination to random sector in universe
			$rating_change = round($targetinfo['rating'] * .1);
			$dest_sector = rand(1,$sector_max - 1);
			$update = $smcFunc['db_query']('', '
				UPDATE {raw:db_ships}
				SET turns = turns - 1,
					turns_used = turns_used + 1,
					rating = rating - {int:rating_change}
				WHERE ship_id = {int:ship_id}',
				array(
					'db_ships' => $dbtables['ships'],
					'rating_change' => $rating_change,
					'ship_id' => $playerinfo['ship_id']
				)
			);
			
			$txt['bnt_att_ewdlog'] = str_replace("[name]", $playerinfo['character_name'], $txt['bnt_att_ewdlog']);
			$txt['bnt_att_ewdlog'] = str_replace("[sector]", $playerinfo['sector'], $txt['bnt_att_ewdlog']);
			
			playerlog($targetinfo['ship_id'], LOG_ATTACK_EWD, "$playerinfo[character_name]");
			
			$update = $smcFunc['db_query']('', '
				UPDATE {raw:db_ships}
				SET sector = {int:dest_sector},
					dev_emerwarp = dev_emerwarp - 1,
					cleared_defences = NULL
				WHERE ship_id = {int:ship_id}',
				array(
					'db_ships' => $dbtables['ships'],
					'dest_sector' => $dest_sector,
					'ship_id' => $targetinfo['ship_id']
				)
			);
			
			log_move($targetinfo['ship_id'], $dest_sector);
			
			echo $txt['bnt_att_ewd'], '<br><br>';
		}
		else
		{
			if ((($targetscore / $playerscore) < $bounty_ratio || $targetinfo['turns_used'] < $bounty_minturns) && $targetinfo['xenobe'] == "N")
			{
				// Check to see if there is Federation bounty on the player. If there is, people can attack regardless.
				$btyamount = 0;
				
				$result = $smcFunc['db_query']('', '
					SELECT SUM(amount) AS btytotal
					FROM {raw:db_bounty}
					WHERE bounty_on = {int:ship_id}
					AND placed_by = 0',
					array(
						'db_bounty' => $dbtables['bounty'],
						'ship_id' => $targetinfo['ship_id']
					)
				);
				
				if ($smcFunc['db_num_rows']($result) > 0)
				{
					$hasbounty = $smcFunc['db_fetch_assoc']($result);
					$smcFunc['db_free_result']($result);
					$btyamount = $hasbounty['btytotal'];
				}
				
				if ($btyamount <= 0)
				{
					$bounty = round(floatval($playerscore * $bounty_maxvalue));
					
					$smcFunc['db_insert']('insert',
						$dbtables['bounty'],
						array(
							'bounty_on' => 'int',
							'placed_by' => 'int',
							'amount' => 'float'
						),
						array(
							$playerinfo['ship_id'],
							0,
							$bounty
						),
						''
					);
					
					playerlog($playerinfo['ship_id'], LOG_BOUNTY_FEDBOUNTY, "$bounty");
					echo $txt['bnt_by_fedbounty2'], '<br><br>';
				}
			}
			
			if ($targetinfo['dev_emerwarp'] > 0)
				playerlog($targetinfo['ship_id'], LOG_ATTACK_EWDFAIL, $playerinfo['character_name']);
			
			$targetenergy = $targetinfo['ship_energy'];
			$playerenergy = $playerinfo['ship_energy'];
			
			// I added these two so we can have a value for debugging and reporting totals
			// if we use the variables in calcs below, change the display of stats too
			$targetbeams = NUM_BEAMS($targetinfo['beams']);
			if ($targetbeams > $targetinfo['ship_energy'])
				$targetbeams = $targetinfo['ship_energy'];
			
			$targetinfo['ship_energy'] = $targetinfo['ship_energy'] - $targetbeams;
			
			// Why dont we set targetinfo[ship_energy] to a variable instead?
			$playerbeams = NUM_BEAMS($playerinfo['beams']);
			if ($playerbeams > $playerinfo['ship_energy'])
				$playerbeams = $playerinfo['ship_energy'];
			
			$playerinfo['ship_energy'] = $playerinfo['ship_energy'] - $playerbeams;
			$playershields = NUM_SHIELDS($playerinfo['shields']);
			if ($playershields > $playerinfo['ship_energy'])
				$playershields = $playerinfo['ship_energy'];
			
			$playerinfo['ship_energy'] = $playerinfo['ship_energy'] - $playershields;
			$targetshields = NUM_SHIELDS($targetinfo['shields']);
			if ($targetshields > $targetinfo['ship_energy'])
				$targetshields = $targetinfo['ship_energy'];
			$targetinfo['ship_energy'] = $targetinfo['ship_energy'] - $targetshields;

			$playertorpnum = round(mypw($level_factor,$playerinfo['torp_launchers'])) * 10;
			if($playertorpnum > $playerinfo['torps'])
				$playertorpnum = $playerinfo['torps'];
			
			$targettorpnum = round(mypw($level_factor,$targetinfo['torp_launchers'])) * 10;
			if($targettorpnum > $targetinfo['torps'])
				$targettorpnum = $targetinfo['torps'];
	  
			$playertorpdmg = $torp_dmg_rate * $playertorpnum;
			$targettorpdmg = $torp_dmg_rate * $targettorpnum;
			$playerarmor = $playerinfo['armor_pts'];
			$targetarmor = $targetinfo['armor_pts'];
			$playerfighters = $playerinfo['ship_fighters'];
			$targetfighters = $targetinfo['ship_fighters'];
			$targetdestroyed = 0;
			$playerdestroyed = 0;
	  
			echo '
			', $txt['bnt_att_att'], ' ', $targetinfo['character_name'], ' ', $txt['bnt_abord'], ' ', $targetinfo['ship_name'], ':<br><br>
			<table width="100%">
				<tr>
					<th width="12%">', $txt['bnt_player'], '</th>
					<th width="12%">', $txt['bnt_cmb_beams'], '(lvl)</th>
					<th width="12%">', $txt['bnt_cmb_shields'], '(lvl)</th>
					<th width="12%">', $txt['bnt_energy'], '(Start)</th>
					<th width="12%">', $txt['bnt_torps'], '(lvl)</th>
					<th width="12%">', $txt['bnt_att_torpdmg'], '</th>
					<th width="12%">', $txt['bnt_fighters'], '(lvl)</th>
					<th width="12%">', $txt['bnt_cmb_armor'], '(lvl)</th>
				</tr>
				<tr>
					<td width="12%">', $txt['bnt_cmb_you'], '</td>
					<td width="12%">', $playerbeams, '(', $playerinfo['beams'], ')', '</td>
					<td width="12%">', $playershields, '(', $playerinfo['shields'], ')', '</td>
					<td width="12%">', $playerinfo['ship_energy'], '(', $playerenergy, ')</td>
					<td width="12%">', $playertorpnum, '(', $playerinfo['torp_launchers'], ')</td>
					<td width="12%">', $playertorpdmg, '</td>
					<td width="12%">', $playerfighters, '</td>
					<td width="12%">', $playerarmor, '(', $playerinfo['armor'], ')</td>
				</tr>
				<tr>
					<td width="12%">', $txt['bnt_att_target'], '</td>
					<td width="12%">', $targetbeams, '(', $targetinfo['beams'], ')</td>
					<td width="12%">', $targetshields, '(', $targetinfo['shields'], ')</td>
					<td width="12%">', $targetinfo['ship_energy'], '(', $targetenergy, ')</td>
					<td width="12%">', $targettorpnum, '(', $targetinfo['torp_launchers'], ')</td>
					<td width="12%">', $targettorpdmg, '</td>
					<td width="12%">', $targetfighters, '</td>
					<td width="12%">', $targetarmor, '(', $targetinfo['armor'], ')</td>
				</tr>
			</table>
			<p>', $txt['bnt_att_targetpod'], ' : ', $targetinfo['dev_escapepod'], '</p>
			<p>', $txt['bnt_att_attackerpod'], ' : ', $playerinfo['dev_escapepod'], '</p><br>
			', $txt['bnt_att_beams'], '<br>';
			
			if ($targetfighters > 0 && $playerbeams > 0)
			{
				if ($playerbeams > round($targetfighters / 2))
				{
					$temp = round($targetfighters / 2);
					$lost = $targetfighters - $temp;
					//
					// DOUBLECHECK - this is not implemented yet
					//
					// Maybe we should report on how many beams fired , etc for comparision/bugtracking?
					echo $targetinfo['character_name'], ' ', $txt['bnt_att_lost'], ' ', $lost, ' ', $txt['bnt_fighters'], '<br>';
					$targetfighters = $temp;
					$playerbeams = $playerbeams - $lost;
				}
				else
				{
					$targetfighters = $targetfighters - $playerbeams;
					echo $targetinfo['character_name'], ' ', $txt['bnt_att_lost'], ' ', $playerbeams, ' ', $txt['bnt_fighters'], '<br>';
					$playerbeams = 0;
				}
			}
			
			if ($playerfighters > 0 && $targetbeams > 0)
			{
				if ($targetbeams > round($playerfighters / 2))
				{
					$temp = round($playerfighters / 2);
					$lost = $playerfighters - $temp;
					echo $txt['bnt_att_ylost'], ' ', $lost, ' ', $txt['bnt_fighters'], '<br>';
					$playerfighters = $temp;
					$targetbeams = $targetbeams - $lost;
				}
				else
				{
					$playerfighters = $playerfighters - $targetbeams;
					echo $txt['bnt_att_ylost'], ' ', $targetbeams, ' ', $txt['bnt_fighters'], '<br>';
					$targetbeams = 0;
				}
			}
			
			if ($playerbeams > 0)
			{
				if ($playerbeams > $targetshields)
				{
					$playerbeams = $playerbeams - $targetshields;
					$targetshields = 0;
					echo $targetinfo['character_name'], ' ', $txt['bnt_att_sdown'], '<br>';
				}
				else
				{
					echo $targetinfo['character_name'], ' ', $txt['bnt_att_shits'], ' ', $playerbeams, ' ', $txt['bnt_att_dmg'], '<br>';
					$targetshields = $targetshields - $playerbeams;
					$playerbeams = 0;
				}
			}
			
			if ($targetbeams > 0)
			{
				if ($targetbeams > $playershields)
				{
					$targetbeams = $targetbeams - $playershields;
					$playershields = 0;
					echo $txt['bnt_att_ydown'], '<br>';
				}
				else
				{
					echo $txt['bnt_att_yhits'], ' ', $targetbeams, ' ', $txt['bnt_att_dmg'], '.<br>';
					$playershields = $playershields - $targetbeams;
					$targetbeams = 0;
				}
			}
		  
			if ($playerbeams > 0)
			{
				if ($playerbeams > $targetarmor)
				{
					$targetarmor = 0;
					echo $targetinfo['character_name'], ' ', $txt['bnt_att_sarm'], ' <br>';
				}
				else
				{
					$targetarmor = $targetarmor - $playerbeams;
					echo $targetinfo['character_name'], ' ', $txt['bnt_att_ashit'], ' ', $playerbeams, ' ', $txt['bnt_att_dmg'], '<br>';
				}
			}
		  
			if ($targetbeams > 0)
			{
				if ($targetbeams > $playerarmor)
				{
					$playerarmor = 0;
					echo $txt['bnt_att_yarm'], '<br>';
				}
				else
				{
					$playerarmor = $playerarmor - $targetbeams;
					echo $txt['bnt_att_ayhit'], ' ', $targetbeams, ' ', $txt['bnt_att_dmg'], '<br>';
				}
			}

			echo '<br>', $txt['bnt_att_torps'], '<br>';
			
			if ($targetfighters > 0 && $playertorpdmg > 0)
			{
				if ($playertorpdmg > round($targetfighters / 2))
				{
					$temp = round($targetfighters / 2);
					$lost = $targetfighters - $temp;
					echo $targetinfo['character_name'], ' ', $txt['bnt_att_lost'], ' ', $lost, ' ', $txt['bnt_fighters'], '<br>';
					$targetfighters = $temp;
					$playertorpdmg = $playertorpdmg - $lost;
				}
				else
				{
					$targetfighters = $targetfighters - $playertorpdmg;
					echo $targetinfo['character_name'], ' ', $txt['bnt_att_lost'], ' ', $playertorpdmg, ' ', $txt['bnt_fighters'], '<br>';
					$playertorpdmg = 0;
				}
			}

			if ($playerfighters > 0 && $targettorpdmg > 0)
			{
				if ($targettorpdmg > round($playerfighters / 2))
				{
					$temp = round($playerfighters / 2);
					$lost = $playerfighters - $temp;
					echo $txt['bnt_att_ylost'], ' ', $lost, ' ', $txt['bnt_fighters'], '<br>';
					echo $temp, ' - ', $playerfighters, ' - ', $targettorpdmg;
					$playerfighters = $temp;
					$targettorpdmg = $targettorpdmg - $lost;
				}
				else
				{
					$playerfighters = $playerfighters - $targettorpdmg;
					echo $txt['bnt_att_ylost'], ' ', $targettorpdmg, ' ', $txt['bnt_fighters'], '<br>';
					$targettorpdmg = 0;
				}
			}
		  
			if ($playertorpdmg > 0)
			{
				if ($playertorpdmg > $targetarmor)
				{
					$targetarmor = 0;
					echo $targetinfo['character_name'], ' ', $txt['bnt_att_sarm'], '<br>';
				}
				else
				{
					$targetarmor = $targetarmor - $playertorpdmg;
					echo $targetinfo['character_name'], ' ', $txt['bnt_att_ashit'], ' ', $playertorpdmg, ' ', $txt['bnt_att_dmg'], '<br>';
				}
			}

			if ($targettorpdmg > 0)
			{
				if ($targettorpdmg > $playerarmor)
				{
					$playerarmor = 0;
					echo $txt['bnt_att_yarm'], '<br>';
				}
				else
				{
					$playerarmor = $playerarmor - $targettorpdmg;
					echo $txt['bnt_att_ayhit'], ' ', $targettorpdmg, ' ', $txt['bnt_att_dmg'], '<br>';
				}
			}
		  
			echo '<br>', $txt['bnt_att_fighters'], '<br>';
		  
			if ($playerfighters > 0 && $targetfighters > 0)
			{
				if ($playerfighters > $targetfighters)
				{
					echo $targetinfo['character_name'], ' ', $txt['bnt_att_lostf'], '<br>';
					$temptargfighters = 0;
				}
				else
				{
					echo $targetinfo['character_name'], ' ', $txt['bnt_att_lost'], ' ', $playerfighters, ' ', $txt['bnt_fighters'], '<br>';
					$temptargfighters = $targetfighters - $playerfighters;
				}
				
				if ($targetfighters > $playerfighters)
				{
					echo $txt['bnt_att_ylostf'], '<br>';
					$tempplayfighters = 0;
				}
				else
				{
					echo $txt['bnt_att_ylost'], ' ', $targetfighters, ' ', $txt['bnt_fighters'], '<br>';
					$tempplayfighters = $playerfighters - $targetfighters;
				}
				
				$playerfighters = $tempplayfighters;
				$targetfighters = $temptargfighters;
			}
		  
			if ($playerfighters > 0)
			{
				if ($playerfighters > $targetarmor)
				{
					$targetarmor = 0;
					echo $targetinfo['character_name'], ' ', $txt['bnt_att_sarm'], '<br>';
				}
				else
				{
					$targetarmor = $targetarmor - $playerfighters;
					echo $targetinfo['character_name'], ' ', $txt['bnt_att_ashit'], ' ', $playerfighters, ' ', $txt['bnt_att_dmg'], '<br>';
				}
			}
		  
			if ($targetfighters > 0)
			{
				if ($targetfighters > $playerarmor)
				{
					$playerarmor = 0;
					echo $txt['bnt_att_yarm'], '<br>';
				}
				else
				{
					$playerarmor = $playerarmor - $targetfighters;
					echo $txt['bnt_att_ayhit'], ' ', $targetfighters, ' ', $txt['bnt_att_dmg'], '<br>';
				}
			}
			
			if ($targetarmor < 1)
			{
				echo '<br>', $targetinfo['character_name'], ' ', $txt['bnt_att_sdest'], '<br>';

				if ($targetinfo['dev_escapepod'] == "Y")
				{
					$rating = round($targetinfo['rating'] / 2);
					echo $txt['bnt_att_espod'], '<br><br>';
					
					$update = $smcFunc['db_query']('', '
						UPDATE {raw:db_ships}
						SET hull = 0,
							engines = 0,
							power = 0,
							sensors = 0,
							computer = 0,
							beams = 0,
							torp_launchers = 0,
							torps = 0,
							armor = 0,
							armor_pts = 100,
							cloak = 0,
							shields = 0,
							sector = 0,
							ship_organics = 0,
							ship_ore = 0,
							ship_goods = 0,
							ship_energy = {int:start_energy},
							ship_colonists = 0,
							ship_fighters = 100,
							dev_warpedit = 0,
							dev_genesis = 0,
							dev_beacon = 0,
							dev_emerwarp = 0,
							dev_escapepod = "N",
							dev_fuelscoop = "N",
							dev_minedeflector = 0,
							on_planet = "N",
							rating = {int:rating},
							cleared_defences = NULL,
							dev_lssd = "N"
						WHERE ship_id = {int:ship_id}',
						array(
							'db_ships' => $dbtables['ships'],
							'ship_id' => $targetinfo['ship_id'],
							'start_energy' => $start_energy,
							'rating' => $rating
						)
					);
					
					playerlog($targetinfo['ship_id'], LOG_ATTACK_LOSE, "$playerinfo[character_name]|Y");
					collect_bounty($playerinfo['ship_id'],$targetinfo['ship_id']);
				}
				else
				{
					playerlog($targetinfo['ship_id'], LOG_ATTACK_LOSE, "$playerinfo[character_name]|N");
					db_kill_player($targetinfo['ship_id']);
					collect_bounty($playerinfo['ship_id'],$targetinfo['ship_id']);
				}

				if ($playerarmor > 0)
				{
					$rating_change = round($targetinfo['rating'] * $rating_combat_factor);
					
					// Updating to always get a positive rating increase for xenobe and the credits they are carrying - rjordan
					$salv_credits = 0;
					
		            if ($targetinfo['xenobe'] == "Y")
		            {
						$update = $smcFunc['db_query']('', '
							UPDATE {raw:db_xenobes}
							SET active = "N"
							WHERE ship_id = {int:ship_id}',
							array(
								'db_xenobes' => $dbtables['xenobes'],
								'ship_id' => $targetinfo['ship_id']
							)
						);
						
						if ($rating_change > 0)
						{
							$rating_change = 0 - $rating_change;
							playerlog($targetinfo['ship_id'], LOG_ATTACK_LOSE, "$playerinfo[character_name]|N");
							collect_bounty($playerinfo['ship_id'],$targetinfo['ship_id']);
							db_kill_player($targetinfo['ship_id']);
						}
						$salv_credits = $targetinfo['credits'];
					}

					$free_ore = round($targetinfo['ship_ore'] / 2);
					$free_organics = round($targetinfo['ship_organics'] / 2);
					$free_goods = round($targetinfo['ship_goods'] / 2);
					$free_holds = NUM_HOLDS($playerinfo['hull']) - $playerinfo['ship_ore'] - $playerinfo['ship_organics'] - $playerinfo['ship_goods'] - $playerinfo['ship_colonists'];

					if ($free_holds > $free_goods)
					{
						$salv_goods = $free_goods;
						$free_holds = $free_holds - $free_goods;
					}
					elseif ($free_holds > 0)
					{
						$salv_goods = $free_holds;
						$free_holds = 0;
					}
					else
						$salv_goods = 0;
		    
					if ($free_holds > $free_ore)
					{
						$salv_ore = $free_ore;
						$free_holds = $free_holds - $free_ore;
					}
					elseif ($free_holds > 0)
					{
						$salv_ore = $free_holds;
						$free_holds = 0;
					}
					else
						$salv_ore = 0;

					if ($free_holds > $free_organics)
					{
						$salv_organics = $free_organics;
						$free_holds = $free_holds - $free_organics;
					}
					elseif ($free_holds > 0)
					{
						$salv_organics = $free_holds;
						$free_holds = 0;
					}
					else
						$salv_organics=0;
					
					$salv_credits = $targetinfo['credits'];
					
					$ship_value = $upgrade_cost * (round(mypw($upgrade_factor, $targetinfo['hull'])) + round(mypw($upgrade_factor, $targetinfo['engines'])) + round(mypw($upgrade_factor, $targetinfo['power']))+round(mypw($upgrade_factor, $targetinfo['computer'])) + round(mypw($upgrade_factor, $targetinfo['sensors'])) + round(mypw($upgrade_factor, $targetinfo['beams'])) + round(mypw($upgrade_factor, $targetinfo['torp_launchers'])) + round(mypw($upgrade_factor, $targetinfo['shields'])) + round(mypw($upgrade_factor, $targetinfo['armor'])) + round(mypw($upgrade_factor, $targetinfo['cloak'])));
					$ship_salvage_rate = rand(10,20);
					// Added credits for xenobe - 0 if normal player - GunSlinger
					$ship_salvage = round($ship_value * $ship_salvage_rate / 100 + $salv_credits);

					$txt['bnt_att_ysalv'] = str_replace("[salv_ore]", $salv_ore, $txt['bnt_att_ysalv']);
					$txt['bnt_att_ysalv'] = str_replace("[salv_organics]", $salv_organics, $txt['bnt_att_ysalv']);
					$txt['bnt_att_ysalv'] = str_replace("[salv_goods]", $salv_goods, $txt['bnt_att_ysalv']);
					$txt['bnt_att_ysalv'] = str_replace("[ship_salvage_rate]", $ship_salvage_rate, $txt['bnt_att_ysalv']);
					$txt['bnt_att_ysalv'] = str_replace("[ship_salvage]", $ship_salvage, $txt['bnt_att_ysalv']);
					$txt['bnt_att_ysalv'] = str_replace("[rating_change]", NUMBER(abs($rating_change)), $txt['bnt_att_ysalv']);
					echo $txt['bnt_att_ysalv'];
					
					$update = $smcFunc['db_query']('', '
						UPDATE {raw:db_ships}
						SET ship_ore = ship_ore + {int:salv_ore}, 
							ship_organics = ship_organics + {int:salv_organics}, 
							ship_goods = ship_goods + {int:salv_goods}, 
							credits = credits + {float:ship_salvage}
						WHERE ship_id = {int:ship_id}',
						array(
							'db_ships' => $dbtables['ships'],
							'ship_id' => $playerinfo['ship_id'],
							'salv_ore' => $salv_ore,
							'salv_organics' => $salv_organics,
							'salv_goods' => $salv_goods,
							'ship_salvage' => $ship_salvage
						)
					);
				  
					$armor_lost = $playerinfo['armor_pts'] - $playerarmor;
					$fighters_lost = $playerinfo['ship_fighters'] - $playerfighters;
					$energy = $playerinfo['ship_energy'];
				
					$update = $smcFunc['db_query']('', '
						UPDATE {raw:db_ships}
						SET ship_energy = {int:energy},
							ship_fighters = ship_fighters - {int:fighters_lost},
							armor_pts = armor_pts - {int:armor_lost}, 
							torps = torps - {int:playertorpnum}, 
							turns = turns - 1, 
							turns_used = turns_used + 1,
							rating = rating - {int:rating_change} 
						WHERE ship_id = {int:ship_id}',
						array(
							'db_ships' => $dbtables['ships'],
							'ship_id' => $playerinfo['ship_id'],
							'energy' => $energy,
							'fighters_lost' => $fighters_lost,
							'armor_lost' => $armor_lost,
							'playertorpnum' => $playertorpnum,
							'rating_change' => $rating_change
						)
					);
					
					echo $txt['bnt_att_ylost'], ' ', $armor_lost, ' ', $txt['bnt_armorpts'], ', ', $fighters_lost, ' ', $txt['bnt_fighters'], ', ', $txt['bnt_att_andused'], ' ', $playertorpnum, ' ', $txt['bnt_torps'], '.<br><br>';
				}
			}
			else
			{
				$txt['bnt_att_stilship'] = str_replace("[name]", $targetinfo['character_name'], $txt['bnt_att_stilship']);
				echo $txt['bnt_att_stilship'], '<br>';
				
				$rating_change = round($targetinfo['rating'] * .1);
				$armor_lost = $targetinfo['armor_pts'] - $targetarmor;
				$fighters_lost = $targetinfo['ship_fighters'] - $targetfighters;
				$energy = $targetinfo['ship_energy'];
				
				$update = $smcFunc['db_query']('', '
					UPDATE {raw:db_ships}
					SET ship_energy = {int:energy},
						ship_fighters = ship_fighters - {int:fighters_lost},
						armor_pts = armor_pts - {int:armor_lost}, 
						torps = torps - {int:playertorpnum}
					WHERE ship_id = {int:ship_id}',
					array(
						'db_ships' => $dbtables['ships'],
						'ship_id' => $targetinfo['ship_id'],
						'energy' => $energy,
						'fighters_lost' => $fighters_lost,
						'armor_lost' => $armor_lost,
						'playertorpnum' => $playertorpnum
					)
				);
				
				$armor_lost = $playerinfo['armor_pts'] - $playerarmor;
				$fighters_lost = $playerinfo['ship_fighters'] - $playerfighters;
				$energy = $playerinfo['ship_energy'];
				
				$update = $smcFunc['db_query']('', '
					UPDATE {raw:db_ships}
					SET ship_energy = {int:energy},
						ship_fighters = ship_fighters - {int:fighters_lost},
						armor_pts = armor_pts - {int:armor_lost}, 
						torps = torps - {int:playertorpnum}, 
						turns = turns - 1, 
						turns_used = turns_used + 1,
						rating = rating - {int:rating_change} 
					WHERE ship_id = {int:ship_id}',
					array(
						'db_ships' => $dbtables['ships'],
						'ship_id' => $playerinfo['ship_id'],
						'energy' => $energy,
						'fighters_lost' => $fighters_lost,
						'armor_lost' => $armor_lost,
						'playertorpnum' => $playertorpnum,
						'rating_change' => $rating_change
					)
				);
			
				echo $txt['bnt_att_ylost'], ' ', $armor_lost, ' ', $txt['bnt_armorpts'], ', ', $fighters_lost, ' ', $txt['bnt_fighters'], ', ', $txt['bnt_att_andused'], ' ', $playertorpnum, ' ', $txt['bnt_torps'], '<br><br>';
			}
			
			// Attacking player lost the battle
			if ($playerarmor < 1)
			{
				echo $txt['bnt_att_yshiplost'], '<br><br>';

				if ($playerinfo['dev_escapepod'] == "Y")
				{
					$rating = round($playerinfo['rating'] / 2);
					$update = $smcFunc['db_query']('', '
						UPDATE {raw:db_ships}
						SET hull = 0,
							engines = 0,
							power = 0,
							sensors = 0,
							computer = 0,
							beams = 0,
							torp_launchers = 0,
							torps = 0,
							armor = 0,
							armor_pts = 100,
							cloak = 0,
							shields = 0,
							sector = 0,
							ship_organics = 0,
							ship_ore = 0,
							ship_goods = 0,
							ship_energy = {int:start_energy},
							ship_colonists = 0,
							ship_fighters = 100,
							dev_warpedit = 0,
							dev_genesis = 0,
							dev_beacon = 0,
							dev_emerwarp = 0,
							dev_escapepod = "N",
							dev_fuelscoop = "N",
							dev_minedeflector = 0,
							on_planet = "N",
							rating = {int:rating},
							cleared_defences = NULL,
							dev_lssd = "N"
						WHERE ship_id = {int:ship_id}',
						array(
							'db_ships' => $dbtables['ships'],
							'ship_id' => $playerinfo['ship_id'],
							'start_energy' => $start_energy,
							'rating' => $rating
						)
					);
					
					collect_bounty($targetinfo['ship_id'],$playerinfo['ship_id']);
				}
				else
				{
					echo $txt['bnt_att_nopod'], ' ', $playerinfo['dev_escapepod'], '<br>';
					db_kill_player($playerinfo['ship_id']);
					collect_bounty($targetinfo['ship_id'],$playerinfo['ship_id']);
				}
				
				// Even thought the attacking ship lost, check to make sure the defending ship also survived.
				if ($targetarmor > 0)
				{
					$free_ore = round($playerinfo['ship_ore'] / 2);
					$free_organics = round($playerinfo['ship_organics'] / 2);
					$free_goods = round($playerinfo['ship_goods'] / 2);
					$free_holds = NUM_HOLDS($targetinfo['hull']) - $targetinfo['ship_ore'] - $targetinfo['ship_organics'] - $targetinfo['ship_goods'] - $targetinfo['ship_colonists'];

					if ($free_holds > $free_goods)
					{
						$salv_goods = $free_goods;
						$free_holds = $free_holds - $free_goods;
					}
					elseif ($free_holds > 0)
					{
						$salv_goods = $free_holds;
						$free_holds = 0;
					}
					else
						$salv_goods = 0;
						
					if ($free_holds > $free_ore)
					{
						$salv_ore = $free_ore;
						$free_holds = $free_holds - $free_ore;
					}
					elseif ($free_holds > 0)
					{
						$salv_ore = $free_holds;
						$free_holds = 0;
					}
					else
						$salv_ore = 0;
			  
					if ($free_holds > $free_organics)
					{
						$salv_organics = $free_organics;
						$free_holds = $free_holds - $free_organics;
					}
					elseif ($free_holds > 0)
					{
						$salv_organics = $free_holds;
						$free_holds = 0;
					}
					else
						$salv_organics = 0;
					
					$salv_credits = $playerinfo['credits'];
					
					$ship_value = $upgrade_cost * (round(mypw($upgrade_factor, $playerinfo['hull'])) + round(mypw($upgrade_factor, $playerinfo['engines'])) + round(mypw($upgrade_factor, $playerinfo['power'])) + round(mypw($upgrade_factor, $playerinfo['computer'])) + round(mypw($upgrade_factor, $playerinfo['sensors'])) + round(mypw($upgrade_factor, $playerinfo['beams'])) + round(mypw($upgrade_factor, $playerinfo['torp_launchers'])) + round(mypw($upgrade_factor, $playerinfo['shields'])) + round(mypw($upgrade_factor, $playerinfo['armor'])) + round(mypw($upgrade_factor, $playerinfo['cloak'])));
					$ship_salvage_rate = rand(10,20);
					// Added credits for xenobe - 0 if normal player - GunSlinger
					$ship_salvage = round($ship_value * $ship_salvage_rate / 100 + $salv_credits);

					$txt['bnt_att_salv'] = str_replace("[salv_ore]", $salv_ore, $txt['bnt_att_salv']);
					$txt['bnt_att_salv'] = str_replace("[salv_organics]", $salv_organics, $txt['bnt_att_salv']);
					$txt['bnt_att_salv'] = str_replace("[salv_goods]", $salv_goods, $txt['bnt_att_salv']);
					$txt['bnt_att_salv'] = str_replace("[ship_salvage_rate]", $ship_salvage_rate, $txt['bnt_att_salv']);
					$txt['bnt_att_salv'] = str_replace("[ship_salvage]", $ship_salvage, $txt['bnt_att_salv']);
					$txt['bnt_att_salv'] = str_replace("[name]", $targetinfo['character_name'], $txt['bnt_att_salv']);
					echo $txt['bnt_att_salv'], '<br>';
					
					$update = $smcFunc['db_query']('', '
						UPDATE {raw:db_ships}
						SET ship_ore = ship_ore + {int:salv_ore}, 
							ship_organics = ship_organics + {int:salv_organics}, 
							ship_goods = ship_goods + {int:salv_goods}, 
							credits = credits + {float:ship_salvage}
						WHERE ship_id = {int:ship_id}',
						array(
							'db_ships' => $dbtables['ships'],
							'ship_id' => $targetinfo['ship_id'],
							'salv_ore' => $salv_ore,
							'salv_organics' => $salv_organics,
							'salv_goods' => $salv_goods,
							'ship_salvage' => $ship_salvage
						)
					);

					$armor_lost = $targetinfo['armor_pts'] - $targetarmor;
					$fighters_lost = $targetinfo['ship_fighters'] - $targetfighters;
					$energy = $targetinfo['ship_energy'];
					
					$update = $smcFunc['db_query']('', '
						UPDATE {raw:db_ships}
						SET ship_energy = {int:energy},
							ship_fighters = ship_fighters - {int:fighters_lost},
							armor_pts = armor_pts - {int:armor_lost}, 
							torps = torps - {int:playertorpnum}
						WHERE ship_id = {int:ship_id}',
						array(
							'db_ships' => $dbtables['ships'],
							'ship_id' => $targetinfo['ship_id'],
							'energy' => $energy,
							'fighters_lost' => $fighters_lost,
							'armor_lost' => $armor_lost,
							'playertorpnum' => $playertorpnum
						)
					);
				}
			}
		}
	}
}

// $unlock = $smcFunc['db_query']('', 'UNLOCK TABLES');

//-------------------------------------------------------------------------------------------------

TextMainMenu();

?>
